#include "canvas.h"
#include "ui_canvas.h"
#include "point3d.h"
#include <QPainter>
#include <QDebug>

Canvas::Canvas(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Canvas)
{
    ui->setupUi(this);

    lastLeft = NULLPOINT;

    buffer = QImage(QSize(50,50), QImage::Format_RGB32);
    QPainter p;
    p.begin(&buffer);
    p.fillRect(buffer.rect(),Qt::white);
    p.end();

    m_L = new QShortcut(Qt::Key_L, this);
    m_Plus = new QShortcut(Qt::Key_Plus, this);
    m_Minus = new QShortcut(Qt::Key_Minus, this);
    m_S = new QShortcut(Qt::Key_S, this);

    connect(ui->ckCustomizedPoints, SIGNAL(clicked()),
            this, SIGNAL(check()));

    connect(ui->ckOriginalShowed, SIGNAL(clicked()),
            this, SIGNAL(check()));

    connect(ui->ckOriginalPoints, SIGNAL(clicked()),
            this, SIGNAL(check()));

    connect(ui->ckCustomizedShowed, SIGNAL(clicked()),
            this, SIGNAL(check()));

    connect(m_L, SIGNAL(activated()),
            this, SIGNAL(l()));

    connect(m_Plus, SIGNAL(activated()),
            this, SIGNAL(plus()));

    connect(m_Minus, SIGNAL(activated()),
            this, SIGNAL(minus()));

    connect(m_S, SIGNAL(activated()),
            this, SIGNAL(s()));

}

bool Canvas::originalShowedCkecked()
{
    return ui->ckOriginalShowed->isChecked();
}

bool Canvas::customizedShowedCkecked()
{
    return ui->ckCustomizedShowed->isChecked();
}

bool Canvas::originalPointsChecked()
{
    return ui->ckOriginalPoints->isChecked();
}

bool Canvas::customizedPointsChecked()
{
    return ui->ckCustomizedPoints->isChecked();
}

Canvas::~Canvas()
{
    delete ui;
    delete m_L;
    delete m_Plus;
    delete m_Minus;
    delete m_S;
}

void Canvas::mousePressEvent(QMouseEvent *e)
{
    lastLeft = ui->label->mapFromGlobal(e->globalPos());
    if(e->button() == Qt::LeftButton)
    {
        emit left(lastLeft);
        e->accept();
    }
    else
    {
        emit right(lastLeft);
        e->ignore();
    }
}

void Canvas::mouseReleaseEvent(QMouseEvent *e)
{
    lastLeft = NULLPOINT;
    if(e->button() == Qt::LeftButton)
    {
        emit leftRelease();
        e->accept();
    }
    else
    {
        emit rightRelease();
        e->ignore();
    }
}

void Canvas::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() == Qt::LeftButton)
    {
        QPoint p = ui->label->mapFromGlobal(e->globalPos());
        QPainter painter;
        painter.begin(&buffer);
        painter.drawLine(lastLeft,p);
        lastLeft = p;
        painter.end();
        paintBuffer();

        emit left(lastLeft);
        e->accept();
    }else if(e->buttons() == Qt::RightButton)
    {
        lastLeft = ui->label->mapFromGlobal(e->globalPos());
        emit right(lastLeft);
    }else
        e->ignore();

}

void Canvas::resizeEvent(QResizeEvent *)
{
    setupBuffer();
}

void Canvas::setupBuffer()
{
    QImage nb(ui->label->size(), QImage::Format_RGB32);
    QPainter p;
    p.begin(&nb);
    p.fillRect(nb.rect(),Qt::white);
    p.drawImage(QPoint(0,0), buffer);
    p.end();

    buffer = nb;
    paintBuffer();
}

void Canvas::clear()
{
    QPainter p;
    p.begin(&buffer);
    p.fillRect(buffer.rect(),Qt::white);
    p.end();

    paintBuffer();
}

void Canvas::paintBuffer()
{
    ui->label->setPixmap(QPixmap::fromImage(buffer));
}

void Canvas::drawCurvePoligonal(const std::vector<Point3D>& points, const QColor& cor , bool withPoints)
{
    if(points.size() < 3)
        return;

    QPen pen;

    pen.setColor(cor);
    pen.setWidth(2);

    QPainter p;
    p.begin(&buffer);
    p.setPen(pen);

    QPoint p1(points[0].x(), points[0].y());
    QPoint p2(points[points.size()-1].x(), points[points.size()-1].y());
    p.drawLine(p1,p2);
    if(withPoints)
    {
        p.drawEllipse(p1, 4, 4);
    }
    for(uint i = 1; i < points.size(); ++i)
    {
        p1 = QPoint(points[i-1].x(), points[i-1].y());
        p2 = QPoint(points[i].x(), points[i].y());
        p.drawLine(p1,p2);
        if(withPoints)
            p.drawEllipse(p2, 4, 4);
    }
    p.end();
    paintBuffer();
}
